﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace ZXCompany.DBUtil
{
    public class Common
    {

        #region 图片上传
        /**//// <summary>
        /// 图片上传
        /// </summary>
        /// <param name="file"></param>
        /// <returns></returns>
        public string UpLoadPic(HttpPostedFileBase file)
        {            
            //try
            //{
            //    string fileName = "";
            //    string strDir = System.Configuration.ConfigurationManager.AppSettings["uploadImage"];
            //    if (file.ContentLength > 0)
            //    {
            //        if (file.ContentLength < 1024 * 200)
            //        {
            //            fileName = DateTime.Now.ToString("yyyyMMddHHmmss") + "." + file.FileName.Split('.')[1];
            //            string strNew =strDir + fileName ;        //新文件名
            //            file.SaveAs(strNew);
            //            //System.Drawing.Image imgPhoto = System.Drawing.Image.FromStream(file.InputStream, true);
            //            //phWidth = imgPhoto.Width;
            //            //phHeight = imgPhoto.Height;
            //            return strNew;
            //        }
            //    }
            //}
            //catch (Exception ex)
            //{
            //    //return "错误：" + ex.Message;
            //}
            //return null;
            try
            {
                //MVC 上传图片 - 海里的贝壳 - apple的博客MVC 上传图片 - 海里的贝壳 - apple的博客 {
                 string fileName = "";
                 string strDir = System.Configuration.ConfigurationManager.AppSettings["uploadImage"];
                 if (file.ContentLength > 0)
                 {
                     //if (file.ContentLength < 1024 * 200)
                     //{
                         string strNew = Guid.NewGuid().ToString("N") + Path.GetExtension(file.FileName); //新文件名
                         fileName = strDir + strNew;
                         file.SaveAs(System.Web.HttpContext.Current.Server.MapPath(fileName));
                         //file.SaveAs(Path.Combine(Path.Combine(System.Web.HttpContext.Current.Request.Path, ""), fileName));
                         //取高和宽
                         System.Drawing.Image imgPhoto = System.Drawing.Image.FromStream(file.InputStream, true);
                         //phWidth = imgPhoto.Width;
                         //phHeight = imgPhoto.Height;
                         return fileName;
                     //}
                     //else
                     //{
                     //   return "错误：图片大小超过指定大小";
                     //}
                 }
                 else
                {
                    return "";
                }
            }
            catch (Exception ex)
            {
                return "错误：" + ex.Message;
            }
        }
        #endregion

        #region 获取图片目录下所有图片
        public List<string> GetImgList()
        {
            List<string> ImgList = new List<string>();
            try
            {
                string strDir = System.Configuration.ConfigurationManager.AppSettings["uploadImage"];
                strDir = System.Web.HttpContext.Current.Server.MapPath(strDir);
                DirectoryInfo directoryInfo = new DirectoryInfo(strDir);
                FileInfo[] dirInfo = directoryInfo.GetFiles();
                foreach (FileInfo fileinfo in dirInfo)
                {

                    string FileType = fileinfo.FullName.Substring(fileinfo.FullName.LastIndexOf("."));
                    //lblchina.Text.ToUpper()  转大写
                    //lblchina.Text..ToLower() 转小写 
                    FileType = FileType.ToLower();
                    string FilePath = fileinfo.Name;
                    if (FileType == ".jpg" || FileType == ".png" || FileType == ".gif" || FileType == ".bmp")
                    {
                        ImgList.Add(FilePath);
                    }
                }
                //if (intlen == 0)
                //{
                //    dRow["Name "] = fileinfo.Name;
                //    dRow["FilePath "] = FilePath;
                //    dTable.Rows.Add(dRow);
                //}
            }
            catch (Exception ex)
            {
                return null;
            }
            return ImgList;
        }
        #endregion

        #region 删除图片
        public bool Img_del( string Imgstr)
        {
            try
            {
                if (Imgstr == "") return true;
                string fileName = "";
                string strDir = System.Configuration.ConfigurationManager.AppSettings["uploadImage"];
                fileName = strDir + Imgstr;
                fileName = System.Web.HttpContext.Current.Server.MapPath(fileName);
                System.IO.FileInfo DeleFile = new System.IO.FileInfo(fileName);
                if (DeleFile.Exists)
                {
                    DeleFile.Delete();
                }
                return true;
            }
            catch (Exception ex)
            {}
            return false;
        }

        #endregion
    }
}